package cc.fs.myproject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

import java.lang.ref.WeakReference;

/**
 * Created by fostion on 2015/6/9.
 */
public class BaseActivity extends AppCompatActivity{

    BaseActivity _this;
    private MyHandler handler = new MyHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _this = this;
    }

    public void runDelayed(Runnable runnable, int delayMillis) {
        Message msg = new Message();
        msg.what = -101;
        msg.arg1 = -102;
        msg.arg2 = -103;
        msg.obj = runnable;
        handler.sendMessageDelayed(msg, delayMillis);
    }

    class MyHandler extends Handler {

        private WeakReference<BaseActivity> mOuter;

        public MyHandler(BaseActivity activity) {
            mOuter = new WeakReference<BaseActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            final BaseActivity outer = mOuter.get();
            if (outer == null) {
                return;
            }
            if(msg.what==-101 && msg.arg1==-102 && msg.arg2==-103) {
                ((Runnable) msg.obj).run();
                return;
            }

            outer.handler(msg);
            super.handleMessage(msg);
        }
    }

    public void handler(Message msg){

    }

}
